﻿using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Linq;
using System.Text;
using DTO;
using System.IO;

namespace DAO
{
    public class TinhThanhDAO : DataProvider
    {
        protected override object GetDataRowFromDataReader(OleDbDataReader dr)
        {
            TinhThanhDTO tinhThanhDTO = new TinhThanhDTO();
            if (!dr.IsDBNull(0))
            {
                tinhThanhDTO.MaTinhThanh = dr.GetInt32(0);
            }
            
            if (!dr.IsDBNull(1))
            {
                tinhThanhDTO.TenTinhThanh = dr.GetString(1);
            }

            return (object)tinhThanhDTO;
        }

        public TinhThanhDTO Search(int id)
        {
            TinhThanhDTO tinhThanhDTO = null;
            try
            {
                string strSQL = @"SELECT *" +
                                @" FROM TINHTHANH" +
                                @" WHERE MaTinhThanh = " + id.ToString();

                using (OleDbDataReader dr = this.ExcuteReader(strSQL))
                {
                    if (dr.Read())
                    {
                        tinhThanhDTO = (TinhThanhDTO)GetDataRowFromDataReader(dr);
                    }
                }
            }
            catch (Exception ex)
            {
                using (FileStream fs = new FileStream("D:\\log.txt", FileMode.Append, FileAccess.Write))
                {
                    fs.Write(Encoding.ASCII.GetBytes(ex.Message), 0, ex.Message.Length);
                }
                tinhThanhDTO = null;
            }

            return tinhThanhDTO;
        }
    }
}
